<template>
  <div class="adduserBody">
    <div style="text-align: center">
      <h3>编辑用户</h3>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" size="small">
      <el-form-item label="登录名" prop="code">
        <el-input v-model="ruleForm.code"></el-input>
      </el-form-item>
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <!--<el-form-item label="重复密码" prop="againPassword">-->
      <!--<el-input type="password" v-model="ruleForm.againPassword"></el-input>-->
      <!--</el-form-item>-->
      <el-form-item label="用户邮箱" prop="emailNumber">
        <el-input v-model="ruleForm.emailNumber"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input v-model="ruleForm.phoneNumber"></el-input>
      </el-form-item>
      <!--<el-form-item label="用户角色" prop="region">-->
      <!--<el-select v-model="ruleForm.region" placeholder="请选择活动区域">-->
      <!--<el-option label="区域一" value="shanghai"></el-option>-->
      <!--<el-option label="区域二" value="beijing"></el-option>-->
      <!--</el-select>-->
      <!--</el-form-item>-->

      <!--<el-form-item label="用户性别" prop="resource">-->
        <!--<el-radio-group v-model="ruleForm.resource">-->
          <!--<el-radio label="男"></el-radio>-->
          <!--<el-radio label="女"></el-radio>-->
        <!--</el-radio-group>-->
      <!--</el-form-item>-->


      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import axios from 'axios'
  import QS from 'qs';
  import { getToken, setToken, removeToken } from '@/utils/auth'
  import { mapState } from 'vuex'

  export default {
    name: "adduser",
    computed:{
      ...mapState([
        'userEditData',
      ]),
    },
    data() {
      return {
        ruleForm: {
          userid:'',
          code: '',
          name: '',
          password: '',
          emailNumber: '',
          phoneNumber: '',
          region: '',
        },
        rules: {
          code: [
            { required: true, message: '请输入登录名', trigger: 'blur' },
            { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' },
            { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
          ],
          // againPassword: [
          //   { required: true, message: '请进行密码确认', trigger: 'blur' },
          //   { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
          // ],
          emailNumber: [
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输正确的邮箱地址', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur' }
          ],
          resource: [
            { required: true, message: '请选择用户性别', trigger: 'change' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          const that = this;   //由于this指向已经不是 vue 的对象啦，所以必须重新对this进行定义。
          if (valid) {
            const userform = {};
            userform['userId'] = this.ruleForm.userid;
            userform['userCode'] = this.ruleForm.code;
            userform['userName'] = this.ruleForm.name;
            userform['userPwd'] = this.ruleForm.password;
            userform['userEmail'] = this.ruleForm.emailNumber;
            userform['userPhone'] = this.ruleForm.phoneNumber;
            userform['userGender'] = this.ruleForm.resource;

            axios.post('/api/sys/sysUser/updateSysUser',QS.stringify(userform)) //需要使用QS组件进行包裹，不然无法传值
              .then(function (response) {
                const data = response.data
                if (data.code == "200"){
                  alert('修改成功');
                  that.$router.push({ path: '/user' });
                }else if (data.code == "500"){
                  alert(data.errorMessage);
                }else{
                  alert('错误代码:' + data.code + '请联系系统管理员！')
                }
              })
              .catch(function (error) {
                console.log(error);
              });

          }else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted:function (){
      this.ruleForm.code = this.userEditData.userCode;
      this.ruleForm.name = this.userEditData.userName;
      this.ruleForm.password = this.userEditData.userPwd;
      this.ruleForm.emailNumber = this.userEditData.userEmail;
      this.ruleForm.phoneNumber = this.userEditData.userPhone;
      this.ruleForm.userid = this.userEditData.userId;
    },
  }
</script>

<style scoped>
  .adduserBody{
    margin: 10px 10px;
    width: 600px;
  }

</style>
